var ResourceList = {
	data:{},
	init: function (){
		this.data = new $.dataManage({key:'resource_id'});
		
		$('#list').treeTable({clickableNodeNames:true});
		$('#list input:checkbox').click(function (e){
			if (this.checked) {
				if ($(this).attr('parentbox')) {
					$('#list input:checkbox[value='+$(this).attr('parentbox')+']').attr('checked', true).triggerHandler('click');
				}
				if (e.ctrlKey) {
					$('#list input:checkbox[parentbox='+this.value+']').attr('checked', true).each(function (){
						$('#list input:checkbox[parentbox='+this.value+']').attr('checked', true);
					});
				}
			} else {
				$('#list input:checkbox[parentbox='+this.value+']').attr('checked', false).each(function (){
					$('#list input:checkbox[parentbox='+this.value+']').attr('checked', false);
				});
			}
		});
		
		$('#refresh').click(function (){
			var self = this;
			$.ajax({
				url:'/system/resource/refresh',
				beforeSend: function (){
					return $(self).lvButtonAjaxLoad('start');
				},
				complete: function (){
					$(self).lvButtonAjaxLoad('stop');
				},
				success: function (rs){
					//setTimeout('location.reload()', 1000);
				}
			});
		});
	},
	
	expandAll: function (){
		$('#list tr:gt(0):visible').each(function (){
			$(this).expand();
			$('.child-of-'+this.id).each(function (){
				$(this).expand();
			});
		});
	},
	
	drawAll: function (){
		$('#list tr:gt(0):visible').removeClass("expanded").each(function (){
			$(this).collapse();
			$('.child-of-'+this.id).removeClass("expanded").each(function (){
				$(this).collapse();
			});
		});
	},
	
	assign: function (obj){
		$.ajax({
			url:'/system/resource/assign',
			data: $('#list input:checkbox').serialize()+'&role_id='+$(obj).prev().val(),
			beforeSend: function (){
				return $(obj).lvButtonAjaxLoad('start');
			},
			complete: function (){
				$(obj).lvButtonAjaxLoad('stop');
			}
		});
	}
};

$(function (){
	ResourceList.init();
});
